#!/bin/bash
# Copyright (c) 2000-2016 Synology Inc. All rights reserved.

if [ -z "$__INCLUDE_PLATFORMS__" ]; then
__INCLUDE_PLATFORMS__=defined

Source "include/variable"

AllPlatformOptionNames="6281 alpine alpine4k apollolake armada370 armada375 armada37xx armada38x armadaxp avoton braswell broadwell broadwellnk bromolow cedarview comcerto2k denverton dockerx64 evansport geminilake grantley hi3535 kvmx64 monaco purley qoriq rtd1296 v1000 x64"

AllPlatforms="	bromolow	BROMOLOW			linux-3.10.x    	Intel Bromolow
		6281		MARVELL_88F6281			linux-2.6.32    	Marvell 88F6281
		x64		X64				linux-3.10.x    	X64
		cedarview	CEDARVIEW			linux-3.10.x    	Intel Cedarview
		qoriq		PPC_QORIQ			linux-2.6.32		POWER PC QorIQ
		armadaxp	MARVELL_ARMADAXP		linux-3.x		Marvell armadaxp
		armada370	MARVELL_ARMADA370		linux-3.x		Marvell armada370
		armada375	MARVELL_ARMADA375		linux-3.x		Marvell armada375
		evansport	EVANSPORT			linux-3.x		Intel Evansport
		comcerto2k	MINDSPEED_COMCERTO2K		linux-3.x		MindSpeed comcerto 2000
		avoton		AVOTON				linux-3.10.x		Intel Avoton
		alpine		ALPINE				linux-3.10.x-bsp	AnnapurnaLabs Alpine
		alpine4k	ALPINE				linux-3.10.x-bsp	AnnapurnaLabs Alpine
		braswell	BRASWELL			linux-3.10.x		Intel Braswell
		monaco  	STM_MONACO			linux-3.10.x-bsp	STM Monaco H412
		armada38x	MARVELL_ARMADA38X		linux-3.10.x-bsp	Marvell armada38x
		hi3535		HISILICON_HI3535		linux-3.4.x		Hisilicon Hi3535
		broadwell	BROADWELL			linux-3.10.x		Intel Broadwell
		dockerx64	DOCKERX64			linux-3.10.x		Synology Docker X64
		kvmx64		KVMX64				linux-3.10.x		Virtual Machine
		grantley	GRANTLEY			linux-3.10.x		Intel Grantley
		apollolake	APOLLOLAKE			linux-4.4.x		Intel Apollo Lake
		rtd1296		REALTEK_RTD1296			linux-4.4.x		Realtek rtd1296
		denverton	DENVERTON			linux-4.4.x		Intel Denverton
		armada37xx	MARVELL_ARMADA37XX		linux-4.4.x		Marvell armada37xx
		broadwellnk	BROADWELLNK			linux-4.4.x		Intel Broadwell with new kernel
		purley		PURLEY				linux-4.4.x		Intel Purley
		v1000		V1000				linux-4.4.x		Intel V1000
		geminilake	GEMINILAKE			linux-4.4.x		Intel Geminilake
		"

#
# Return name list of all supported platforms
#
AllPlatformOptions () {
	echo "$AllPlatformOptionNames"
}

Is64BitPlatform() {
	[ "$PLATFORM_FAMILY" = "SYNOPLAT_F_X86_64" ] || [ "$PLATFORM_FAMILY" = "SYNOPLAT_F_ARMV8" ]
}


# check build platform and set the corresponding variables:
# PLATFORM_ABBR, BUILD_TARGET, BUILD_OPT
#
# Usage
#	AskPlatfrom $@
AskPlatform()
{
#				PLATFORM_ABBR	BUILD_TARGET			kernel_version		Comment
	local INDEX="" i=1 abbr target kernel comment others
	declare -a AbbrArray TargetArray
	# init array & hash table
	line=`echo "$AllPlatforms"| wc -l`
	for (( i=1; i <= line; i++ )); do
		abbr=`echo "$AllPlatforms" | awk '(NR=='$i'){print $1}'`
		target=`echo "$AllPlatforms" | awk '(NR=='$i'){print $2}'`
		declare AbbrHash_$abbr=$i
		declare TargetHash_$target=$i
		AbbrArray[$i]=$abbr
		TargetArray[$i]=$target
	done

	if [ $# -gt 0 ]; then
		for ThisArg in $@; do
			# Remember platform choice
			INDEX=AbbrHash_${ThisArg#--*}
			if [ -n "${!INDEX}" ]; then
				PLATFORM_ABBR=${ThisArg#--*}
				BUILD_TARGET=${TargetArray[${!INDEX}]}
				BUILD_OPT="--${PLATFORM_ABBR}"
			fi
		done
	fi
	if [ -z "${!INDEX}" -a -f /env32.mak ]; then
		# Auto Detect the platform in env32.mak (genreated by SynoBuild & SynoGPLBuild)
		declare `grep ^SYNO_PLATFORM= /env32.mak`
		INDEX=TargetHash_${SYNO_PLATFORM}
		if [ -n "${!INDEX}" ]; then
			PLATFORM_ABBR="${AbbrArray[${!INDEX}]}"
			BUILD_TARGET=$SYNO_PLATFORM
			BUILD_OPT="--${PLATFORM_ABBR}"
		else
			echo "Failed to detect platform ($SYNO_PLATFORM) in env32.mak"
		fi
		# Since alpine family share the same SYNO_PLATFORM, the above table scanning
		# falls back to the latest ALPINE entry (alpine4k), we have to use another
		# way to distinguish them
		if [ "${BUILD_TARGET}" = "ALPINE" ]; then
			local platform="alpine"
			cat /root/.bashrc | grep "PS1" | grep "alpine4k" > /dev/null
			if [ $? -eq 0 ]; then
				platform="alpine4k"
			fi
			PLATFORM_ABBR="${platform}"
			BUILD_OPT="--${platform}"
		fi
	fi

	# Ask platform interactively if not specified in argument
	if [ -z "$BUILD_TARGET" ]; then
		echo "Please choose target platform:"
		i=0
		echo "$AllPlatforms" | while read abbr target kernel comment; do
			printf "\t%4s %-30s%-20s%-20s\n" \
			"[$((i+=1))]" "$comment" "($kernel)" "{--$abbr}"
		done
		echo -en "Enter a number: "
		read Reply;
		if [ -z "${AbbrArray[$Reply]}" ]; then
			echo "Unknow platform. EXIT!!"
			exit 1
		fi
		PLATFORM_ABBR="${AbbrArray[${Reply}]}"
		BUILD_TARGET=${TargetArray[$Reply]}
		BUILD_OPT="--${PLATFORM_ABBR}"
	fi
}

#
# Return name list of all supported platforms, comma separated
#
AllPlatformOptionsComma ()
{
	local RetString
	for PLATFORM in $AllPlatformOptionNames; do
		RetString="${RetString}${PLATFORM},"
	done
	echo $RetString
}

#
# Check if $1 is a supported platform name.
# Support both forms of "platform" and "--platform".
#
# Returns 0 if true, else 1
#
IsPlatformOption ()
{
	if [ $# -gt 1 ]; then
		return 1
	fi
	for PLATFORM in $AllPlatformOptionNames; do
		if [ "$1" = "$PLATFORM" ]; then
			return 0
		fi
		# Handle arg with "--" too
		if [ "$1" = "--$PLATFORM" ]; then
			return 0
		fi
	done
	# not matching any of known platforms
	return 1
}

##########################################################################
# Read platform config file {{{
#
LoadPlatformRelated()
{
	[ "$BUILD_TARGET" = "CHROOT" ] && return 0

	SYNO_KERNEL_SOURCE_DIR="`${ScriptsDir}/ProjectDepends.py -dp ${PLATFORM_ABBR}`"
	if [ -z "${SYNO_KERNEL_SOURCE_DIR}" ]; then
		echo "Error: Failed to match kernel dir ?!"
		echo "ScriptsDir=${ScriptsDir}, PLATFORM_ABBR=${PLATFORM_ABBR}"
		return 1
	fi

	if ! source ${ScriptsDir}/include/platform.${PLATFORM_ABBR}; then
		echo "Failed to include ${ScriptsDir}/include/platform.${PLATFORM_ABBR}"
		return 1
	fi
	KernelDir="/source/${SYNO_KERNEL_SOURCE_DIR}"

	return 0
}

PrepareKernelEnv()
{
	cp -vf "$SynobiosDir/include/synobios.h" include/linux/synobios.h
}

fi
# vim:ft=sh
